# 提取和解析数据
import requests
from lxml import etree
# 数据写入CSV文件,生成表格
import csv
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                  "AppleWebKit/537.36 (HTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
}
url = "https://www.zbj.com/fw/?type=new&kw=saas"
resp = requests.get(url, headers=headers, verify=False)
# print(resp.text)

# 解析
html = etree.HTML(resp.text)

# 初始化一个列表来存储数据
data = []
divs = html.xpath('//*[@id="__layout"]/div/div[3]/div[1]/div[4]/div/div[2]/div[1]/div[2]/div')
for div in divs:
    # [0].strip("¥") 从列表取出数据并去掉¥
    price = div.xpath("./div/div[3]/div[1]/span/text()")[0].strip("¥")
    title = "saas".join(div.xpath("./div/div[3]/div[2]/a/span/text()"))
    com_name = div.xpath("./div/div[5]/div/div/div/text()")[0]
    # 数据添加到列表
    data.append([com_name, title, price])
# 将数据写入CSV文件
with open('zbj.csv', 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    # 写入表头
    writer.writerow(['公司名称', '业务范围', '价格'])
    # 写入数据行
    for row in data:
        writer.writerow(row)
print("数据已成功写入CSV文件。")